package com.wechat.service.account.starter.customMenu;

import com.alibaba.fastjson.JSONObject;
import com.wechat.service.account.dto.customMenu.MenuCreateReqDTO;
import com.wechat.service.account.starter.AccessTokenService;
import com.wechat.service.account.starter.BaseWechatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @Author yanggld
 * @Date 2019/7/10-18:10
 * @Description
 * 自定义菜单创建接口
 */
@Slf4j
@Service
public class MenuCreateService extends BaseWechatService {

	public boolean createMenu(MenuCreateReqDTO dto){
		log.info("********创建菜单参数:{}********",JSONObject.toJSONString(dto));
		ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(buildCreateMenUrl(), dto, JSONObject.class);
		JSONObject body = responseEntity.getBody();
		log.info("********创建菜单结果:{}********",body);
		if (0 == body.getInteger("errcode")) {
			return true;
		}
		return false;
	}

	public String buildCreateMenUrl(){
		return "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+accessTokenService.getAccessToken();
	}
}
